package com.zhku.yygh.hosp.controller.api;

import com.zhku.yygh.common.config.YyghException;
import com.zhku.yygh.common.helper.HttpRequestHelper;
import com.zhku.yygh.common.result.Result;
import com.zhku.yygh.common.result.ResultCodeEnum;
import com.zhku.yygh.common.util.MD5;
import com.zhku.yygh.hosp.service.DepartmentService;
import com.zhku.yygh.hosp.service.HospitalService;
import com.zhku.yygh.hosp.service.HospitalSetService;
import com.zhku.yygh.hosp.service.ScheduleService;
import com.zhku.yygh.model.hosp.Department;
import com.zhku.yygh.model.hosp.Hospital;
import com.zhku.yygh.model.hosp.Schedule;
import com.zhku.yygh.vo.hosp.DepartmentQueryVo;
import com.zhku.yygh.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @BelongsProject: yygh-parent
 * @BelongsPackage: com.zhku.yygh.hosp.controller.api
 * @Author: "deweyMDH"
 * @CreateTime: 2023-05-14  11:11
 * @Version: 1.0
 */
@RestController
@RequestMapping(value = "api/hosp")
public class ApiController {

    @Autowired
    HospitalService hospitalService;

    @Autowired
    HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    // 一个公共的前置数据处理，可以用aop来做
    private void Datautil(HttpServletRequest request, Map<String, Object> paramMap){

        // 1 获取医院系统传递过来的签名， 签名进行MD5加密
        String hospSign = (String) paramMap.get("sign");

        // 2 根据查询过来的医院编码，查询数据库，查询签名
        String hoscode = (String)paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);

        // 3 把数据库查询签名做MD5解密
        String signKeyMd5 = MD5.encrypt(signKey);

        // 4 判断是否签名一致
        if(!hospSign.equals(signKeyMd5)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
    }

    // 上传医院接口
    @PostMapping("saveHospital")
    public Result saveHospital(HttpServletRequest request){
        // 获取传过来的医院参数
        Map<String, String[]> parameterMap = request.getParameterMap();
        // 调用工具类，把参数做转换，加密，什么的
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);

        // 调用一个私有方法处理
        this.Datautil(request, paramMap);

        // 传输过程中，“+” 转换为了 “ ”， 要转换回来
        String localData = (String) paramMap.get("logoData");
        localData = localData.replaceAll(" ", "+");
        paramMap.put("logoData", localData);

        // 上述过程，就是把 manage 项目的数据库中hospital数据，查到 service_hosp 数据库的对应数据，如果一致的话，
        // 就加入到 service_hosp 项目的mongodb数据库中

        // 调用service的方法
        hospitalService.save(paramMap);
        return Result.ok();
    }

    // 查询医院信息
    @ApiOperation(value = "获取医院信息")
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request) {
        // 获取传过来的医院参数
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());

        // 调用一个私有方法处理
        this.Datautil(request, paramMap);

        // 医院编号
        String hoscode = (String) paramMap.get("hoscode");

        // 调用service 方法实现医院编号查询
        Hospital hospital = hospitalService.getByHoscode(hoscode);

        return Result.ok(hospital);
    }

    // 上传科室信息
    @ApiOperation(value = "上传科室")
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        // 获取传过来的医院参数
        Map<String, String[]> parameterMap = request.getParameterMap();
        // 调用工具类，把参数做转换，加密，什么的
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);

        this.Datautil(request, paramMap);

        // 调用service 方法
        departmentService.save(paramMap);

        return Result.ok();
    }

    // 查询科室接口
    @ApiOperation(value = "获取分页列表")
    @PostMapping("department/list")
    public Result department(HttpServletRequest request) {
        // 获取传过来的医院参数
        Map<String, String[]> parameterMap = request.getParameterMap();
        // 调用工具类，把参数做转换，加密，什么的
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);

        // 医院编号和科室编号
        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String)paramMap.get("depcode");
        // 当前页 和 每页记录数
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String)paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String)paramMap.get("limit"));

        this.Datautil(request, paramMap);

        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        departmentQueryVo.setDepcode(depcode);

        // 调用service 方法
        Page<Department> pageModel = departmentService.findPageDepartment(page, limit, departmentQueryVo);
        return Result.ok(pageModel);
    }

    // 删除科室接口
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        // 获取传过来的医院参数
        Map<String, String[]> parameterMap = request.getParameterMap();
        // 调用工具类，把参数做转换，加密，什么的
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);

        this.Datautil(request, paramMap);

        // 医院编号和科室编号
        String hoscode = (String)paramMap.get("hoscode");
        String depcode = (String)paramMap.get("depcode");


        departmentService.remove(hoscode, depcode);
        return Result.ok();
    }

    //上传排班信息
    @ApiOperation(value = "上传排班")
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());

        this.Datautil(request, paramMap);

        scheduleService.save(paramMap);
        return Result.ok();
    }

    // 查询排班信息
    @ApiOperation(value = "获取排班分页列表")
    @PostMapping("schedule/list")
    public Result schedule(HttpServletRequest request){
        // 获取传过来的医院参数
        Map<String, String[]> parameterMap = request.getParameterMap();
        // 调用工具类，把参数做转换，加密，什么的
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);

        // 医院编号和科室编号
        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String)paramMap.get("depcode");
        // 当前页 和 每页记录数
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String)paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String)paramMap.get("limit"));

        this.Datautil(request, paramMap);

        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);

        // 调用service 方法
        Page<Schedule> pageModel = scheduleService.findPageSchedule(page, limit, scheduleQueryVo);
        return Result.ok(pageModel);
    }

    // 删除排班
    @ApiOperation(value = "删除排班")
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request){
        // 获取传过来的医院参数
        Map<String, String[]> parameterMap = request.getParameterMap();
        // 调用工具类，把参数做转换，加密，什么的
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);

        this.Datautil(request, paramMap);

        // 医院编号和排班编号
        String hoscode = (String)paramMap.get("hoscode");
        String hosScheduleId = (String)paramMap.get("hosScheduleId");

        scheduleService.remove(hoscode, hosScheduleId);
        return Result.ok();
    }

}
